<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'卡片列表'),
	array('op'=>'edit','title'=>'添加卡片'),	
);
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','type','endtime','starttime','total');
	$list=pdo_getslice('jy_xunichong_card',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$v){
				if($v['endtime']<time()){
					$v['status']=2;
				}elseif($v['starttime']>time()){
					$v['status']=0;
				}else{
					$v['status']=1;
				}
				$v['endtime']=date('Y-m-d H:i:s',$v['endtime']);
			}
		}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'total','title'=>'总量','type'=>'string'),
			array('type'=>'radio','title'=>'商品类型','name'=>'type','options'=>'credit_type'),
			array('name'=>'endtime','title'=>'结束时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),
		'action'=>array(	
			array('type'=>'post','op'=>'down_code','title'=>'下载兑换码'),
			array('action'=>'/merch.code/','title'=>'兑换码','type'=>'href'),
		)),
		'search_one'=>array('name'=>'title','title'=>'标题'),
	);
	$return['credit_type']=$this->credit_type;
	$return['status']=array('0'=>'未开始','1'=>'进行中','2'=>'已结束');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='down_code'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择兑换活动','error','list');	
	}
	$code=pdo_getall('jy_xunichong_code',array('cardid'=>$id),array('key'));
	$string="把下面的兑换码，提供给印刷商，批量打印出二维码。\r\n";
	foreach($code as $item){
		$string.=custom_url('jy_xunichong/merch_card//code='.$item['key'],true)."\r\n";
	}
	$code_file=pdo_getcolumn('jy_xunichong_card',array('id'=>$id),'code_file');
	if(!$code_file){
		$code_file = ROOT_AT.random(10).".zip";
		$zip = new ZipArchive();
		$zip->open($code_file,ZipArchive::CREATE);
		$zip->addFromString('code.txt',$string);
		$zip->close();
		$zip=cfc('files')->source_save($code_file,'zip','zip');
		$file=tomedia($zip);
		pdo_update('jy_xunichong_card',array('code_file'=>$zip),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$file=tomedia($code_file);
	}
	exi('','','',$file);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_xunichong_card',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['time']=array('tend'=>$item['endtime'],'tstart'=>$item['starttime']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回卡片列表'),
		),
		'edit_tab'=>true,
		'edit'=>array(
					array('name'=>'title','title'=>'标题','type'=>'string'),
					array('type'=>'radio','title'=>'商品类型','name'=>'type','options'=>'credit_type'),
					array('name'=>'time','title'=>'兑换时间','type'=>'time_range','help'=>'[<red>温馨提示</red>]：兑换时间，以生成的兑换码有效期为准！'),
					array('name'=>'balance_type','title'=>'兑换模式','type'=>'radio','options'=>'balance_type'),
					array('name'=>'balance','title'=>'指定面值','type'=>'string','show'=>array('balance_type'=>0)),
					array('name'=>'balance_left','title'=>'最低面值','type'=>'string','show'=>array('balance_type'=>1)),
					array('name'=>'balance_right','title'=>'最高面值','type'=>'string','show'=>array('balance_type'=>1)),
					array('name'=>'rule','title'=>'兑换规则','type'=>'text'),
					
		)
	);
	$return['credit_type']=$this->credit_type;
	$return['balance_type']=array('0'=>'指定额度兑换','1'=>'随机额度兑换');
	$return['showcount']=array('0'=>'不显示','1'=>'显示');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请设置名称','error');
	}
	if(!$postdata['type']){
		exi('请设置类型','error');
	}
	if(!$postdata['balance_type'] && !$postdata['balance']){
		exi('请填写面值','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'type'=>$postdata['type'],
		'starttime'=>$postdata['time']['tstart'],
		'endtime'=>$postdata['time']['tend'],
		'rule'=>$postdata['rule'],
		'balance_type'=>$postdata['balance_type'],
		'balance_left'=>$postdata['balance_left'],
		'balance_right'=>$postdata['balance_right'],
		'balance'=>$postdata['balance']
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_xunichong_card',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		pdo_insert('jy_xunichong_card',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_xunichong_card',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}


